## Table H.5:  Firm and Employee Donations and Lobbying of Specific 
# Politicians, First-Time-Ever Donation, with Lagged Donations: ----------------

## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to main folder
# Step 2: Adjust DATA_DIR to where data is is located
# Step 3: Adjust FIG_DIR to where output folder is
# Step 4: Run entire script
## setup -----------------------------------------------------------------------

# clean slate
rm(list = ls())

# load packages
library(dplyr)
library(stargazer)
library(lfe)
library(tidyr)
options(scipen=999)

# set directory
MAIN_DIR <- "C:/Users/js.egb/Dropbox/campaign-lobby-paper/replication_package"

# set data folder
DATA_DIR <-  paste(MAIN_DIR, "/data/", sep = "")

# set output folder
FIG_DIR <- paste(MAIN_DIR, "/output/", sep = "")


#subset to manufacturing industries
naics_sub <- c("31", "32", "33") # set industries to be analyzed

# file to be read in
#file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.csv") # file to be input
file_in <- paste0(DATA_DIR, "don_lob_all_ind_20210203.rds") # file to be input


# read data
don_lob <- readRDS(file_in) %>%
  filter(naics2 %in% naics_sub) %>%
  mutate(l_sale = log(sale+1), 
         l_emp = log(emp+1), 
         l_don_tot = log(don_tot+1)) %>%
  select(gvkey_govtrack_id, gvkey, govtrack_id, year, naics, naics3, naics2, chamber, 
         lob_any_bin, lob_spon_bin, lob_cosp_bin, lob_comm_bin,
         don_any_bin, treat_don_all,
         don_tot, l_don_tot,
         l_sale, l_emp, # firm covariates
         majority, democrat, com_chair, power_com) %>% # recodings
  mutate(gvkey_govtrack_id_numeric = as.integer(as.factor(gvkey_govtrack_id)),
         year = as.integer(year),
         l_treat_don_all = ifelse(treat_don_all == 1, l_don_tot, 0)) 

gc()
gc()


# Regression models, binary donation indicator, first donation ever, lagged doantions, w/o controls for firm size
lm1 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm2 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm3 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm4 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()

# Regression models, binary donation indicator, first donation ever, lagged doantions, WITH controls for firm size
lm5 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com + l_sale + l_emp | gvkey | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm6 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com + l_sale + l_emp | gvkey + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm7 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com + l_sale + l_emp | gvkey + year + govtrack_id | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
lm8 <- lfe::felm(lob_any_bin ~ treat_don_all + lag(treat_don_all, 1) + lag(treat_don_all, 2) + lag(treat_don_all, 3) + majority + democrat + com_chair + power_com + l_sale + l_emp | gvkey_govtrack_id + year | 0 | gvkey, data = don_lob, keepX=FALSE, keepCX=FALSE, keepModel=FALSE) 
gc()


stargazer::stargazer(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8, type = "latex", title = "Firm and Employee Donations and Lobbying of Specific Politicians with Lagged Donations: The level of analysis is the firm-politician-year, and the data include all publicly traded manufacturing firms (NAICS 2-digit codes 31-33) and members of Congress between 2008 and 2017. The dependent variable is a dummy which equals 1 if firm $i$ lobbied the legislative activity of a politician $j$ in year $t$. $Donationt_{t-k}$ equals 1 if firm $i$ donated to a politician $j$ for the first time in year $t$ and up to year $t-k$, and 0 otherwise. The coefficients come from ordinary least squares regressions, using combinations of firm, politician, year, and firm-politician fixed effects. The columns 5 to 8 include time-varying politician covariates (party, majority status, committee chairmanship, membership of powerful committee), and firm covariates (logged sales, logged number of employees). The standard errors are clustered by firm.",
                     label = "tab_h5",
                     out = paste0(FIG_DIR, "table_h5.tex"),
                     covariate.labels = c("$Donation_{t}$", "$Donation_{t-1}$", "$Donation_{t-2}$", "$Donation_{t-3}$", 
                                          "$Majority_{t}$", "$Democrat_{t}$", 
                                          "$Committee\\;Chair_{t}$", "$Powerful\\;Committee_{t}$",
                                          "$Logged \\;Sales_{t}$", "$Logged\\;Employees_{t}$"), 
                     dep.var.labels = c("Lobbying Legislative Activity$_{t}$"),
                     add.lines = list(
                       c("Firm FEs",  rep(c("$\\checkmark$", "$\\checkmark$", "$\\checkmark$", ""), 2)),
                       c("Year FEs",  rep(c("", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"), 2)),
                       c("Politician FEs",rep(c("", "", "$\\checkmark$", ""), 2)),
                       c("Firm-Politician FEs",rep(c("", "", "", "$\\checkmark$"), 2))),
                     omit.stat = c("rsq", "ser"),
                     column.sep.width = "3pt", notes.align = "l")

rm(lm1, lm2, lm3, lm4, lm5, lm6, lm7, lm8)
gc()





